package com.leyou.upload.controller;

import com.leyou.upload.service.UploadService;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@Controller
@RequestMapping("/upload")
public class UploadController {

    @Resource
    UploadService uploadService;

    /**
     * 上传图片
     * @param file
     * @return
     */
    @PostMapping(value = "/image" ,produces = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<String> uploadImage(@RequestParam("file") MultipartFile file){
          String url = this.uploadService.uploadImage(file);
//          if (StringUtils.isNotBlank(url)){
//              return ResponseEntity.badRequest().build();
//          }
          return ResponseEntity.status(HttpStatus.CREATED).body(url);
    }
}
